package com.sikulifang.bank.simulation.task;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ZipUtil;
import cn.hutool.db.Entity;
import com.sikulifang.bank.simulation.enums.BankEnum;
import com.sikulifang.bank.simulation.service.IDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.List;

/**
 * ClassName SchedulerTask
 * Description 定时任务
 *
 * @author Yat
 * Date 2023年6月28日, 0028 16:49
 * version 1.0
 */
@Component
public class SchedulerTask {

    private IDetailService detailService;


    @Scheduled(cron = "0 1 1 * * ?")
    private void process() {
        final String path = "/data/sftp/sklf/upload/";

        String tranDate = DateUtil.format(DateUtil.yesterday(), DatePattern.PURE_DATE_PATTERN);

        List<Entity> byBankCode = detailService.findByBankCode(BankEnum.CITIC.name(), tranDate);

        if (null == byBankCode || byBankCode.isEmpty()) {
            return;
        }

        File mkdir = FileUtil.mkdir(path + "1_" + tranDate + "_1_");
        for (Entity entity : byBankCode) {
            String pdf = detailService.createPdf(entity);
            FileUtil.copy(pdf, mkdir.getPath(), Boolean.TRUE);
        }

        ZipUtil.zip(mkdir);

        FileUtil.del(mkdir);

    }


    @Autowired
    public void setDetailService(IDetailService detailService) {
        this.detailService = detailService;
    }
}
